package com.diandian.backend.douyin.life.exchange.controller;

import com.diandian.backend.douyin.life.exchange.service.DouyinLifeTokenBusinessService;
import com.diandian.base.boot.starter.exception.DefaultException;
import com.diandian.base.boot.starter.response.BaseResult;
import com.diandian.base.boot.starter.util.ResultUtil;
import com.diandian.douyin.life.boot.starter.dto.response.QueryOnlineOrderPageResponseDataOrder;
import com.diandian.douyin.life.boot.starter.service.order.DouyinLifeOrderBasicService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/order")
public class DouyinLifeOrderQueryController {

    private static final Logger logger = LoggerFactory.getLogger(DouyinLifeOrderQueryController.class);

    private SimpleDateFormat sdfAll = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private DouyinLifeOrderBasicService douyinLifeOrderBasicService;

    @Autowired
    private DouyinLifeTokenBusinessService tokenBusinessService;

    /**
     * 查询指定日期到抖音订单
     * @return
     */
    @GetMapping("/query/time")
    public BaseResult queryByTime(@RequestHeader(value = "clientKey") String clientKey,
                                  @RequestHeader(value = "clientSecret") String clientSecret,
                                  @RequestHeader(value = "account") String account,
                                  @RequestParam(value = "date") String date) {

        logger.info("接收到查询指定日期到抖音订单请求，参数：{} | {} | {} | {}", clientKey, clientSecret, account, date);

        Date startTime = null;
        Date endTime = null;
        try{
            startTime = sdfAll.parse(date + " 00:00:00");
            endTime = sdfAll.parse(date + " 23:59:59");
        } catch (Exception e) {
            logger.error("时间转换异常", e);
        }
        if(startTime == null || endTime == null){
            throw new DefaultException("日期格式不合法");
        }

        String token = tokenBusinessService.getToken(clientKey, clientSecret);
        List<QueryOnlineOrderPageResponseDataOrder> dataList = douyinLifeOrderBasicService.queryOrderAllWithPage(token, account, endTime.getTime() / 1000, startTime.getTime() / 1000);
        if(dataList == null || dataList.size() == 0) {
            return null;
        }

//        for (int i = 0; i < dataList.size(); i++) {
//            List<QueryOnlineOrderPageResponseDataOrderCertificate> certificate = dataList.get(i).getCertificate();
//            if(certificate == null || certificate.size() == 0) {
//                logger.info("第{}条数据中券信息为空：{}", i + 1, JSON.toJSONString(dataList.get(i)));
//            }
//        }

        return ResultUtil.makeResult(dataList);
    }

    /**
     * 根据订单号查询订单数据
     * @param orderNum
     * @return
     */
    @GetMapping("/query/num")
    public BaseResult queryByNum(@RequestHeader(value = "clientKey") String clientKey,
                             @RequestHeader(value = "clientSecret") String clientSecret,
                             @RequestHeader(value = "account") String account,
                             @RequestParam(value = "orderNum") String orderNum) {
        logger.info("接收到根据订单号查询订单数据请求，参数：{} | {} | {} | {}", clientKey, clientSecret, account, orderNum);
        String token = tokenBusinessService.getToken(clientKey, clientSecret);
        return ResultUtil.makeResult(douyinLifeOrderBasicService.queryByOrderNum(token, account, orderNum));
    }

    /**
     * 获取昨天的开始时间
     * @return
     */
//    private Date getLastDayBeginTime(){
//        Calendar calendar = Calendar.getInstance();
//        calendar.add(Calendar.DATE, -1);
//        calendar.set(Calendar.HOUR_OF_DAY, 0);
//        calendar.set(Calendar.MINUTE, 0);
//        calendar.set(Calendar.SECOND, 0);
//        calendar.set(Calendar.MILLISECOND, 0);
//        return calendar.getTime();
//    }

    /**
     * 获取昨天的结束时间
     */
//    private Date getLastDayEndTime(){
//        Calendar calendar = Calendar.getInstance();
//        calendar.add(Calendar.DATE, -1);
//        calendar.set(Calendar.HOUR_OF_DAY, 23);
//        calendar.set(Calendar.MINUTE, 59);
//        calendar.set(Calendar.SECOND, 59);
//        calendar.set(Calendar.MILLISECOND, 0);
//        return calendar.getTime();
//    }

}
